package com.jy.func.scroll;

import java.util.ArrayList;

import com.jy.func.parallax.DemoParallaxFragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;


public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<ScreenSlidePageFragment> mFragments;
    private ViewPager mPager;
	
	public ScreenSlidePagerAdapter(FragmentManager fm) {
		super(fm);
		// TODO Auto-generated constructor stub
		 mFragments = new ArrayList<ScreenSlidePageFragment>();
	}

	@Override
	public Fragment getItem(int i) {
        return mFragments.get(i);
    }

	@Override
	public int getCount() {
        return mFragments.size();
    }

	 public void add(ScreenSlidePageFragment parallaxFragment) {
	        //parallaxFragment.setAdapter(this);
	        mFragments.add(parallaxFragment);
	        notifyDataSetChanged();
	        mPager.setCurrentItem(getCount() - 1, true);

	    }

	    public void remove(int i) {
	        mFragments.remove(i);
	        notifyDataSetChanged();
	    }

	    public void remove(DemoParallaxFragment parallaxFragment) {
	        mFragments.remove(parallaxFragment);

	        int pos = mPager.getCurrentItem();
	        notifyDataSetChanged();

	        mPager.setAdapter(this);
	        if (pos >= this.getCount()) {
	            pos = this.getCount() - 1;
	        }
	        mPager.setCurrentItem(pos, true);

	    }

	    public int getItemPosition(Object object) {
	        return POSITION_NONE;
	    }

	    public void setPager(ViewPager pager) {
	        mPager = pager;
	    }
}
